package client

import (
	"openeuler.org/cir/models"
	"regexp"
)

var (
	RegexpCveNumber            = regexp.MustCompile(`漏洞编号[:：](?s:(.*?))漏洞归属组件[:：]`)
	RegexpCveComponents        = regexp.MustCompile(`漏洞归属组件[:：](?s:(.*?))漏洞归属版本[:：]`)
	RegexpCveVersion           = regexp.MustCompile(`漏洞归属版本[:：](?s:(.*?))CVSS V3.0分值[:：]`)
	RegexpCveScore             = regexp.MustCompile(`CVSS V3.0分值[:：](?s:(.*?))Vector[:：]`)
	RegexpCveVector            = regexp.MustCompile(`Vector[:：](?s:(.*?))(漏洞简述[:：])`)
	RegexpCveBriefDesc         = regexp.MustCompile(`漏洞简述[:：](?s:(.*?))影响性分析说明[:：]`)
	RegexpCveInfluencesDesc    = regexp.MustCompile(`影响性分析说明[:：](?s:(.*?))原理分析[:：]`)
	RegexpCvePrincipleDesc     = regexp.MustCompile(`原理分析[:：](?s:(.*?))openEuler评分[:：]`)
	RegexpCveOpScore           = regexp.MustCompile(`openEuler评分[:：](?s:(.*?))openEulerVector[:：]`)
	RegexpCveOpScore1          = regexp.MustCompile(`openEuler评分[:：](?s:(.*?))受影响版本[:：]`)
	RegexpCveOpVector          = regexp.MustCompile(`openEulerVector[:：](?s:(.*?))受影响版本[:：]`)
	RegexpCveInfluencesVersion = regexp.MustCompile(`受影响版本[:：](?s:(.*?))规避方案或消减措施[:：]`)
	RegexpCvePlannedMeasures   = regexp.MustCompile(`规避方案或消减措施[:：](?s:(.*?))受影响的包[:：]`)
	RegexpCvePlannedMeasures1  = regexp.MustCompile(`规避方案或消减措施[:：](?s:(.*?))$`)
	RegexpCveInfluencesPkg     = regexp.MustCompile(`受影响的包[:：](?s:(.*?))$`)
	RegexpDigital              = regexp.MustCompile(`(\d){1,}(\.\d+)?`)
   //`/AV:[NLAP]/AC:[HL]/PR:[NLH]/UI:[NR]/S:[UC]/C:[NLH]/I:[HNL]/A:[LNH]`
	RegexpVector               = regexp.MustCompile(`AV:[NLAP](?s:(.*?))/A:[LNH]`)
)

//IssueToLoopHole 将issue解析成漏洞
func IssueToLoopHole(body string) (models.Loophole, bool) {
	lp := models.Loophole{}
	if body == "" {
		return lp,false
	}
	sm := RegexpCveNumber.FindAllStringSubmatch(body, -1)
	if len(sm) > 0 &&len(sm[0])>0{
		lp.Number = TrimString(sm[0][1])
	}
	sm = RegexpCveComponents.FindAllStringSubmatch(body, -1)
	if len(sm) > 0 &&len(sm[0])>0{
		lp.Components = TrimString(sm[0][1])
	}
	sm = RegexpCveVersion.FindAllStringSubmatch(body, -1)
	if len(sm) > 0 &&len(sm[0])>0{
		lp.Version = TrimString(sm[0][1])
	}
	sm = RegexpCveScore.FindAllStringSubmatch(body, -1)
	if len(sm) > 0 &&len(sm[0])>0{
		if cs := ExtractDigital(sm[0][1]); cs != "" {
			lp.CvsScore = cs
		}
	}
	sm = RegexpCveVector.FindAllStringSubmatch(body, -1)
	if len(sm) > 0 &&len(sm[0])>0{
		if v := ExTractVector(sm[0][1]); v != "" {
			lp.CvsVector = v
		}
	}
	sm = RegexpCveBriefDesc.FindAllStringSubmatch(body, -1)
	if len(sm) > 0 &&len(sm[0])>0{
		//lp.BriefIntroduction = TrimString(sm[0][1])
		lp.BriefIntroduction = sm[0][1]
	}
	sm = RegexpCveInfluencesDesc.FindAllStringSubmatch(body, -1)
	if len(sm) > 0 &&len(sm[0])>0{
		lp.Influences = TrimString(sm[0][1])
	}
	sm = RegexpCvePrincipleDesc.FindAllStringSubmatch(body, -1)
	if len(sm) > 0 &&len(sm[0])>0{
		//lp.Principle = TrimString(sm[0][1])
		lp.Principle = sm[0][1]
	}
	sm = RegexpCveOpScore.FindAllStringSubmatch(body, -1)
	if len(sm) > 0 {
		if cs := ExtractDigital(sm[0][1]); cs != "" {
			lp.OpScore = cs
		}
	} else {
		sm = RegexpCveOpScore1.FindAllStringSubmatch(body, -1)
		if len(sm) > 0 {
			if cs := ExtractDigital(sm[0][1]); cs != "" {
				lp.OpScore = cs
			}
		}
	}
	sm = RegexpCveOpVector.FindAllStringSubmatch(body,-1)
	if len(sm) >0 &&len(sm[0])>0{
		if v:= ExTractVector(sm[0][1]);v != ""{
			lp.OpVector = v
		}
	}
	sm = RegexpCveInfluencesVersion.FindAllStringSubmatch(body,-1)
	if len(sm) >0 &&len(sm[0])>0{
		lp.InfVersion = TrimString(sm[0][1])
	}
	sm = RegexpCvePlannedMeasures.FindAllStringSubmatch(body,-1)
    if len(sm) >0 {
    	if len(sm[0])>0{
			lp.AvoidScheme = sm[0][1]
			//lp.AvoidScheme = TrimString(sm[0][1])
		}

	}else {
		sm = RegexpCvePlannedMeasures1.FindAllStringSubmatch(body,-1)
		if len(sm) >0 &&len(sm[0])>0{
			//lp.AvoidScheme = TrimString(sm[0][1])
			lp.AvoidScheme = sm[0][1]
		}
	}
	sm = RegexpCveInfluencesPkg.FindAllStringSubmatch(body,-1)
	if len(sm) >0 {
		lp.InfPackage = TrimString(sm[0][1])
	}
	return lp, true
}

func ExtractDigital(body string) string {
	if body == "" {
		return body
	}
	sds := RegexpDigital.FindAllStringSubmatch(body, -1)
	if len(sds) > 0 {
		return sds[0][0]
	}
	return ""
}

func ExTractVector(body string) string {
	if body == "" {
		return body
	}
	rvs := RegexpVector.Find([]byte(body))
	if rv:= string(rvs);rv != "" {
		return rv
	}
	return ""
}
